package org.bsplus.domain;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import org.bsplus.util.security.UserDetailsImpl;
import org.springframework.security.core.context.SecurityContextHolder;

@Entity
public class User extends AbstractEntity
{
	@Column(unique = true, nullable = false)
	private String username;
	@Basic(optional = false)
	private String password;
	@Basic(optional = false)
	private String firstName;
	@Basic(optional = false)
	private String lastName;
	@Basic(optional = false)
	private String emailAddress;
	@ManyToOne(optional = false)
	private Role role;
	@OneToMany
	private Set<Project> projects = new HashSet<Project>();

	/**
	 * @return the username
	 */
	public String getUsername()
	{
		return username;
	}

	/**
	 * @param username
	 *            the username to set
	 */
	public void setUsername(String username)
	{
		this.username = username;
	}

	/**
	 * @return the password
	 */
	public String getPassword()
	{
		return password;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password)
	{
		this.password = password;
	}

	/**
	 * @return the firstName
	 */
	public String getFirstName()
	{
		return firstName;
	}

	/**
	 * @param firstName
	 *            the firstName to set
	 */
	public void setFirstName(String firstName)
	{
		this.firstName = firstName;
	}

	/**
	 * @return the lastName
	 */
	public String getLastName()
	{
		return lastName;
	}

	/**
	 * @param lastName
	 *            the lastName to set
	 */
	public void setLastName(String lastName)
	{
		this.lastName = lastName;
	}

	/**
	 * @return the emailAddress
	 */
	public String getEmailAddress()
	{
		return emailAddress;
	}

	/**
	 * @param emailAddress
	 *            the emailAddress to set
	 */
	public void setEmailAddress(String emailAddress)
	{
		this.emailAddress = emailAddress;
	}

	/**
	 * @return the role
	 */
	public Role getRole()
	{
		return role;
	}

	/**
	 * @param role
	 *            the role to set
	 */
	public void setRole(Role role)
	{
		this.role = role;
	}

	/**
	 * @return the projects
	 */
	public Set<Project> getProjects()
	{
		return projects;
	}

	/**
	 * @param projects
	 *            the projects to set
	 */
	public void setProjects(Set<Project> projects)
	{
		this.projects = projects;
	}

	/**
	 * Convenience method to get the currenl user
	 * 
	 * @return current user
	 */
	public static User getCurrentUser()
	{
		UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
				.getContext().getAuthentication().getPrincipal();
		return userDetails.getUser();
	}
}
